package com.cby.widget.sample.recyclerview

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.cby.widget.lib.kotlin.*

/**
 * @description: RecyclerAutoDeleteActivity
 *
 * @author ChenBaoyang
 * @date 2021/10/16 10:15
 */
class RecyclerAutoDeleteFragment : Fragment() {

    private lateinit var recyclerView: RecyclerView
    private val contentView by lazy {

        RecyclerView{
            layout_width = match_parent
            layout_height = match_parent
            recyclerView = this
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return contentView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        recyclerView.layoutManager = LinearLayoutManager(activity)

        // 添加测试数据
        val beans = ArrayList<BaseItemBean>()
        for (i in 0..100) {
            // 计算终止时间，这里都是当前时间 + i乘以10s
            val terminalTime = System.currentTimeMillis() + i * 10_000
            // 这里手动计算了ViewType (i%2)+1
            beans.add(BaseItemBean(i.toLong(), terminalTime, (i % 2) + 1))
        }

        val adapter = Adapter(beans)
        adapter.onItemDeleteClick = { position ->
            // 点击就删除
            beans.removeAt(position)
            adapter.notifyItemRemoved(position)
        }
        recyclerView.adapter = adapter
    }

}